<template>
  <div class="quickbird-quickHdConfig-set gray-box top-redius border-bottom-gray">
    <div class="title">
      {{ $t('sysManagement.quickHdConfig') }}
    </div>
    <el-form
      ref="dataFormRef"
      label-width="200px"
      class="set-form"
      label-position="left"
      :rules="dataRule"
      :model="dataForm"
      @submit.prevent
      @keyup.enter="onSubmit(dataFormRef)"
    >
      <el-form-item
        :label="`${$t('sysManagement.quick100')} customer:`"
        style="width:640px"
        prop="customer"
      >
        <el-input
          v-model="dataForm.customer"
          :placeholder="`${$t('sysManagement.quick100')} customer`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="`${$t('sysManagement.quick100')} key:`"
        style="width:640px"
        prop="key"
      >
        <el-input
          v-model="dataForm.key"
          :placeholder="`${$t('sysManagement.quick100')}  key`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="`${$t('sysManagement.quick100')} secret:`"
        style="width:640px"
        prop="secret"
      >
        <el-input
          v-model="dataForm.secret"
          :placeholder="`${$t('sysManagement.quick100')}  secret`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="`${$t('sysManagement.quick100')} reqUrl:`"
        style="width:640px"
        prop="reqUrl"
      >
        <el-input
          v-model="dataForm.reqUrl"
          :placeholder="`${$t('sysManagement.quick100')} reqUrl`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="`${$t('sysManagement.quick100')} 电子面单ReqUrl：:`"
        style="width:640px"
        prop="printReqUrl"
      >
        <el-input
          v-model="dataForm.printReqUrl"
          :placeholder="`${$t('sysManagement.quick100')}  电子面单ReqUrl`"
          controls-position="right"
        />
      </el-form-item>
      <el-form-item
        :label="`${$t('sysManagement.printMock')} :`"
        style="width:640px"
        prop="mock"
      >
        <el-switch
          v-model="dataForm.mock"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <el-form-item
        :label="`${$t('sysManagement.isPrint')}`"
        style="width:640px"
        prop="isPrint"
      >
        <el-switch
          v-model="dataForm.isPrint"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <el-form-item
        :label="$t('sysManagement.isQuery')"
        style="width:640px"
        prop="isOpen"
      >
        <el-switch
          v-model="dataForm.isOpen"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <div
        class="default-btn"
        @click="onSubmit(dataFormRef)"
      >
        {{ $t('sysManagement.save') }}
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ElMessage } from 'element-plus'
const emit = defineEmits(['activationConfig'])
const dataFormRef = ref(null)
const props = defineProps({
  saveUrl: {
    type: String,
    default: '/sys/pconfig/save'
  },
  changeFlag: {
    type: Boolean,
    default: false
  }
})

const dataForm = ref({
  customer: null,
  key: null,
  secret: null,
  reqUrl: null,
  printReqUrl: null,
  mock: false,
  isPrint: false,
  isOpen: false
})
const activationEventOriginate = ref(false)

const dataRule = {
  customer: [
    { required: true, message: `${$t('sysManagement.quick100')}  customer ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  key: [
    { required: true, message: `${$t('sysManagement.quick100')}  key ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  secret: [
    { required: true, message: `${$t('sysManagement.quick100')}  secret ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  reqUrl: [
    { required: true, message: `${$t('sysManagement.quick100')}  reqUrl ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ],
  printReqUrl: [
    { required: true, message: `${$t('sysManagement.quick100')}  电子面单ReqUrl ${$t('sysManagement.nullTips')}`, trigger: 'blur' }
  ]
}

watch(
  () => props.changeFlag,
  () => {
    if (!activationEventOriginate.value) {
      dataForm.value.isOpen = false
    }
    activationEventOriginate.value = false
  }
)

onMounted(() => {
  init()
})

const init = () => {
  nextTick(() => {
    http({
      url: http.adornUrl('/sys/pconfig/info/' + 'QUICK100_CONFIG'),
      method: 'get',
      params: http.adornParams()
    }).then(({ data }) => {
      if (data) {
        dataForm.value = JSON.parse(data)
      }
    })
    dataFormRef.value?.resetFields()
  })
}
// 表单提交
const onSubmit = async formEl => {
  if (!formEl) return
  await formEl.validate(valid => {
    if (!valid) return
    http({
      url: http.adornUrl(props.saveUrl),
      method: 'post',
      data: http.adornData({
        paramKey: 'QUICK100_CONFIG',
        paramValue: JSON.stringify(dataForm.value),
        remark: '快递100物流配置'
      })
    }).then(({ data }) => {
      if (data) init()
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          if (dataForm.value.isOpen) {
            activationEventOriginate.value = true
            emit('activationConfig')
          }
        }
      })
    })
  })
}

</script>

<style lang="scss" scoped>
@use '../index.scss';
</style>
